<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * @var Zend_Log
     */
    protected $_logger;

    /**
     * @var Zend_Application_Module_Autoloader
     */
    protected $_resourceLoader;

    /**
     * @var Zend_Controller_Front
     */
    public $frontController;

    /**
     * Setup the logging
     */
    protected function _initLogging()
    {
         $this->bootstrap('frontController');
//       $this->frontController = $this->getResource('frontController');
        $logger = new Zend_Log();

        $writer = 'production' == $this->getEnvironment() ?
			new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/logs/app.log') :
			new Zend_Log_Writer_Firebug();
        $logger->addWriter($writer);
        
        $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
        $logger->addFilter($filter);

        $this->_logger = $logger;
        //$logger->log($message, $priority);
        Zend_Registry::set('log', $logger);
    }

     /**
     * Setup the view
     */
    protected function _initViewSettings()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $this->bootstrap('view');

        $this->_view = $this->getResource('view');

        // add global helpers
        $this->_view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Zend_View_Helper');

        // set encoding and doctype
        $this->_view->setEncoding('UTF-8');
        $this->_view->doctype('XHTML1_STRICT');

        // set the content type and language
        $this->_view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8');
        $this->_view->headMeta()->appendHttpEquiv('Content-Language', 'en-US');

        // set css links and a special import for the accessibility styles
        // $this->_view->headStyle()->setStyle('@import "/css/style_all.css";');
        $this->_view->headLink()->appendStylesheet('/css/style1.css');
        $this->_view->headLink()->appendStylesheet('/css/style_all.css');
        

        // setting the site in the title
        $this->_view->headTitle('Online Will');

        // setting a separator string for segments:
        $this->_view->headTitle()->setSeparator(' - ');
    }

    /**
     * Setup locale
     */
    protected function _initLocale()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $locale = new Zend_Locale('en_US');
        Zend_Registry::set('Zend_Locale', $locale);
    }

    /**
     * Setup the database profiling
     */
    protected function _initDbProfiler()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        if ('production' !== $this->getEnvironment()) {
            $this->bootstrap('db');
            $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
            $profiler->setEnabled(true);
            $this->getPluginResource('db')->getDbAdapter()->setProfiler($profiler);
        }
    }

    /**
     * Add the config to the registry
     */
    protected function _initConfig()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);
        Zend_Registry::set('config', $this->getOptions());
    }

     /**
     * Add required routes to the router
     */
    protected function _initRoutes()
    {
        
        $this->_logger->info('Bootstrap ' . __METHOD__);
        $this->bootstrap('frontController');

        $router = $this->frontController->getRouter();

        // dashboard url router
        $route = new Zend_Controller_Router_Route_Static(
            'dashboard',
            array(
                'action'     => 'index',
                'controller' => 'index',
                'module'     => 'dashboard',
                'isAdmin'    => true
            )
        );
        $router->addRoute('dashboard', $route);

        // logout url router
        $route = new Zend_Controller_Router_Route_Static(
            'logout',
            array(
                'action'     => 'logout',
                'controller' => 'index',
                'module'     => 'main',
                'isAdmin'    => true
            )
        );
        $router->addRoute('logout', $route);

        /* USER MANAGEMENT ROUTERS*/
         // manage-users url router
        $route = new Zend_Controller_Router_Route_Static(
            'manage-users',
            array(
                'action'     => 'list',
                'controller' => 'index',
                'module'     => 'main',
                'isAdmin'    => true
            )
        );
        $router->addRoute('manage-users', $route);

         //edit-users url router
        $route = new Zend_Controller_Router_Route(
            'edit-user/:id',
            array(
                'action'     => 'edit',
                'controller' => 'index',
                'module'     => 'main',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('edit-user', $route);

         //delete-users url router
        $route = new Zend_Controller_Router_Route(
            'delete-user/:id',
            array(
                'action'     => 'delete',
                'controller' => 'index',
                'module'     => 'main',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('delete-user', $route);

        //new-users url router
        $route = new Zend_Controller_Router_Route_Static(
            'new-user',
            array(
                'action'     => 'new',
                'controller' => 'index',
                'module'     => 'main',
            )
        );
        $router->addRoute('new-user', $route);

         //save-users url router
        $route = new Zend_Controller_Router_Route_Static(
            'save-user',
            array(
                'action'     => 'save',
                'controller' => 'index',
                'module'     => 'main',
            )
        );
        $router->addRoute('save-user', $route);

        /* CATEGORY ROUTERS*/
        // new-category url router
        $route = new Zend_Controller_Router_Route_Static(
            '/new-category',
            array(
                'action'     => 'new',
                'controller' => 'index',
                'module'     => 'category',
            )
        );
        $router->addRoute('new-category', $route);

        // edit-category url router
        $route = new Zend_Controller_Router_Route(
            '/edit-category/:id',
            array(
               'action'      => 'edit',
                'controller' => 'index',
                'module'     => 'category',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('edit-category', $route);

         // edit-category url router
        $route = new Zend_Controller_Router_Route(
            '/delete-category/:id',
            array(
               'action'      => 'delete',
                'controller' => 'index',
                'module'     => 'category',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('delete-category', $route);

        // save-category url router
        $route = new Zend_Controller_Router_Route_Static(
            '/save-category',
            array(
               'action'      => 'save',
                'controller' => 'index',
                'module'     => 'category',
            )
        );
        $router->addRoute('save-category', $route);

        /* SUB-CATEGORY ROUTERS*/
        // manage-subcat url router
        
        $route = new Zend_Controller_Router_Route(
            '/manage-subcat/:ident',
            array(
                'action'     => 'index',
                'controller' => 'subcategory',
                'module'     => 'category',
                'ident'      => '',
            ),
            array(
                    'ident' => '[a-zA-Z-_0-9]+',
                )
        );
        $router->addRoute('manage-subcat', $route);
        

        // new-category url router
        $route = new Zend_Controller_Router_Route_Static(
            '/new-subcat',
            array(
                'action'     => 'new',
                'controller' => 'subcategory',
                'module'     => 'category',
            )
        );
        $router->addRoute('new-subcat', $route);

        // edit-category url router
        $route = new Zend_Controller_Router_Route(
            '/edit-subcat/:id',
            array(
               'action'      => 'edit',
               'controller' => 'subcategory',
                'module'     => 'category',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('edit-subcat', $route);

         // edit-category url router
        $route = new Zend_Controller_Router_Route(
            '/delete-subcat/:id',
            array(
               'action'      => 'delete',
                'controller' => 'subcategory',
                'module'     => 'category',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('delete-subcat', $route);

        // save-category url router
        $route = new Zend_Controller_Router_Route_Static(
            '/save-subcat',
            array(
               'action'      => 'save',
                'controller' => 'subcategory',
                'module'     => 'category',
            )
        );
        $router->addRoute('save-subcat', $route);

        /* SUBCATEGORY - LABELS ROUTERS*/
        // manage-labels url router
        $route = new Zend_Controller_Router_Route_Static(
            '/manage-labels',
            array(
                'action'     => 'index',
                'controller' => 'label',
                'module'     => 'category',
            )
        );
        $router->addRoute('manage-labels', $route);

        // new-label url router
        $route = new Zend_Controller_Router_Route_Static(
            '/new-label',
            array(
                'action'     => 'new',
                'controller' => 'label',
                'module'     => 'category',
            )
        );
        $router->addRoute('new-label', $route);

        $route = new Zend_Controller_Router_Route_Static(
            '/save-label',
            array(
                'action'     => 'save',
                'controller' => 'label',
                'module'     => 'category',
            )
        );
        $router->addRoute('save-label', $route);

        // edit label url router
        $route = new Zend_Controller_Router_Route(
            '/edit-label/:id',
            array(
               'action'      => 'edit',
                'controller' => 'label',
                'module'     => 'category',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('edit-label', $route);

         // delete label url router
        $route = new Zend_Controller_Router_Route(
            '/delete-label/:id',
            array(
               'action'      => 'delete',
                'controller' => 'label',
                'module'     => 'category',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('delete-label', $route);

         // getting subcategory by cat id url router
        $route = new Zend_Controller_Router_Route(
            '/get-subcat/:catid',
            array(
               'action'      => 'get-subcategory',
                'controller' => 'label',
                'module'     => 'category',
                'catid'     => ''
            ),
             array('catid' => '[0-9]+')
        );
        $router->addRoute('get-subcat', $route);

        /*URLs FOR MANAGING RELATIONS */
         // manage-relations url router
        $route = new Zend_Controller_Router_Route_Static(
            '/manage-relations',
            array(
               'action'      => 'index',
                'controller' => 'relation',
                'module'     => 'main',
            )
        );
        $router->addRoute('manage-relations', $route);

        // new-relation url router
        $route = new Zend_Controller_Router_Route_Static(
            '/new-relation',
            array(
               'action'      => 'new',
                'controller' => 'relation',
                'module'     => 'main',
            )
        );
        $router->addRoute('new-relation', $route);

        // edit-relation url router
        $route = new Zend_Controller_Router_Route(
            '/edit-relation/:id',
            array(
               'action'      => 'edit',
                'controller' => 'relation',
                'module'     => 'main',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('edit-relation', $route);

        // delete-relation url router
        $route = new Zend_Controller_Router_Route(
            '/delete-relation/:id',
            array(
               'action'      => 'delete',
                'controller' => 'relation',
                'module'     => 'main',
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('delete-relation', $route);

        /*URLs FOR MANAGING Asset form data */
         // manage-asset-form url router
        $route = new Zend_Controller_Router_Route_Static(
            '/manage-asset-form',
            array(
                'action'      => 'index',
                'controller' => 'asset',
                'module'     => 'dataform',
            )
        );
        $router->addRoute('manage-asset-form', $route);

         // edit-relation url router
        $route = new Zend_Controller_Router_Route(
            '/edit-asset-data/:id',
            array(
               'action'      => 'edit',
                'controller' => 'asset',
                'module'     => 'dataform',
                'id'     => ''
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('edit-asset-data', $route);

         // edit-relation url router
        $route = new Zend_Controller_Router_Route(
            '/delete-asset-data/:id',
            array(
               'action'      => 'delete',
                'controller' => 'asset',
                'module'     => 'dataform',
                'id'     => ''
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('delete-asset-data', $route);

         /*URLs FOR MANAGING Liability form data */
         // manage-liability-form url router
        $route = new Zend_Controller_Router_Route_Static(
            '/manage-liability-form',
            array(
                'action'      => 'index',
                'controller' => 'liability',
                'module'     => 'dataform',
            )
        );
        $router->addRoute('manage-liability-form', $route);

         // edit-liability-form url router
        $route = new Zend_Controller_Router_Route(
            '/edit-liability-data/:id',
            array(
               'action'      => 'edit',
                'controller' => 'liability',
                'module'     => 'dataform',
                'id'     => ''
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('edit-liability-data', $route);

         // edit-liability-form url router
        $route = new Zend_Controller_Router_Route(
            '/delete-liability-data/:id',
            array(
               'action'      => 'delete',
                'controller' => 'liability',
                'module'     => 'dataform',
                'id'     => ''
            ),
             array('id' => '[0-9]+')
        );
        $router->addRoute('delete-liability-data', $route);

    }

    /**
     * Configure the default modules autoloading, here we first create
     * a new module autoloader specifiying the base path and namespace
     * for our default module. This will automatically add the default
     * resource types for us. We also add two custom resources for Services
     * and Model Resources.
     */
    protected function _initDefaultModuleAutoloader()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Main',
            'basePath'  => APPLICATION_PATH . '/modules/main',
        ));
        $this->_resourceLoader->addResourceTypes(array(
            'modelResource' => array(
              'path'      => 'models/resources',
              'namespace' => 'Resource',
            ),

            'form' => array(
                    'path' => 'forms',
                    'namespace' => 'Form',
               ),

            'service' => array(
                    'path' => 'services',
                    'namespace' => 'Service',
                    ),

        ));
        
    }
    /**
     * fucntion autoloads the different modules 
     */
    
    protected function _initAllModuleAutoloader()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Category',
            'basePath'  => APPLICATION_PATH . '/modules/category',
        ));
        $this->_resourceLoader->addResourceTypes(array(
            'modelResource' => array(
              'path'      => 'models/resources',
              'namespace' => 'Resource',
            ),

            'form' => array(
                    'path' => 'forms',
                    'namespace' => 'Form',
               ),

        ));

        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Dataform',
            'basePath'  => APPLICATION_PATH . '/modules/dataform',
        ));
        $this->_resourceLoader->addResourceTypes(array(
            'modelResource' => array(
              'path'      => 'models/resources',
              'namespace' => 'Resource',
            ),

            'form' => array(
                    'path' => 'forms',
                    'namespace' => 'Form',
               ),

        ));

    }

 
     /**
     * Add Controller Action Helpers
     */
    protected function _initActionHelpers()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);
       // Zend_Controller_Action_HelperBroker::addHelper(new WOW_Controller_Helper_Acl());
       // Zend_Controller_Action_HelperBroker::addHelper(new SF_Controller_Helper_RedirectCommon());
        // Zend_Controller_Action_HelperBroker::addHelper(new SF_Controller_Helper_Service());
    }

}